package util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "Filter_1",urlPatterns = {"/*"})
public class Filter_1 implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        chain.doFilter(req, resp);
        System.out.println("Filter 1 - encoding begins");
        HttpServletRequest request =(HttpServletRequest) req;
        HttpServletResponse response =(HttpServletResponse) resp;
        String path = request.getRequestURI();
        String token = request.getHeader("token");
        if(!path.contains("login")&&token!=null) {
            String jwt_str = TokenHelper.verify(token, response).toString();
            System.out.println(jwt_str);
            response.getWriter().println(jwt_str);
        }

        System.out.println("Filter 1 - encoding ends");
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
